package router

import (
	_ "gin_server/docs"
	"gin_server/internal/controller"
	"gin_server/internal/middleware"
	"gin_server/internal/model"
	"gin_server/internal/repository"
	"gin_server/internal/service"
	"gin_server/pkg/database"

	"github.com/gin-contrib/cors"
	"github.com/gin-gonic/gin"
	swaggerFiles "github.com/swaggo/files"
	ginSwagger "github.com/swaggo/gin-swagger"
)

func SetupRouter() *gin.Engine {
	router := gin.Default()
	// 配置CORS中间件
	router.Use(cors.New(cors.Config{
		AllowOrigins:     []string{"http://localhost:8080"}, // 允许所有来源，生产环境中应该设置具体的域名
		AllowMethods:     []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
		AllowHeaders:     []string{"Origin", "Content-Type", "Content-Length", "Accept-Encoding", "Authorization"},
		ExposeHeaders:    []string{"Content-Length"},
		AllowCredentials: true,
	}))

	// 依赖注入
	GroupRepo := repository.NewGroupRepository(database.GetDB())
	GroupService := service.NewGroupService(GroupRepo)
	GroupController := controller.NewGroupController(GroupService)

	UserRepo := repository.NewUserRepository(database.GetDB())
	UserService := service.NewUserService(UserRepo)
	UserController := controller.NewUserController(UserService)
	AppUserController := controller.NewAppUserController(UserService)
	LoginController := controller.NewLoginController(UserService)

	// 初始化验证码服务和控制器
	CaptchaService := service.NewCaptchaService()
	CaptchaController := controller.NewCaptchaController(CaptchaService)

	// 初始化邮件服务和控制器
	EmailService := service.NewEmailService()
	EmailController := controller.NewEmailController(EmailService, *CaptchaService)

	// 初始化上传服务和控制器
	UploadService := service.NewUploadService()
	UploadController := controller.NewUploadController(UploadService)

	// swagger文档
	router.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))

	// API 路由组
	api := router.Group("/api")

	// 公开路由（无需认证）
	public := api.Group("/")
	{
		// 验证码相关路由
		public.GET("captcha", CaptchaController.GenerateCaptcha)
		// 注册
		public.POST("app/user/reg", AppUserController.Register)
		// 登录
		public.POST("login/password", LoginController.Login)
		// 刷新token
		public.POST("login/refresh", LoginController.RefreshToken)
		// 发送验证码
		public.GET("email/code", EmailController.SendVerificationCode)
	}

	// 需要认证的路由
	auth := api.Group("/")
	auth.Use(middleware.Auth())
	{
		// 文件上传路由
		auth.POST("upload/file", UploadController.UploadFile)

		// 用户相关路由
		user := auth.Group("/user", middleware.RoleAuth(model.UserRoleTypeAdmin))
		{
			user.GET("", UserController.GetUserList)
			user.GET("/:id", UserController.GetUser)
			user.POST("", UserController.CreateUser)
			user.PUT("/:id", UserController.UpdateUser)
			user.DELETE("/:id", UserController.DeleteUser)
			user.PUT("/:id/restore", UserController.RestoreUser)
			user.DELETE("/:id/force", UserController.ForceDeleteUser)
		}

		// 群组相关路由
		group := auth.Group("/group", middleware.RoleAuth(model.UserRoleTypeAdmin))
		{
			group.GET("", GroupController.GetGroupList)
			group.GET("/:id", GroupController.GetGroup)
			group.POST("", GroupController.CreateGroup)
			group.PUT("/:id", GroupController.UpdateGroup)
			group.DELETE("/:id", GroupController.DeleteGroup)
			group.PUT("/:id/restore", GroupController.RestoreGroup)
			group.DELETE("/:id/force", GroupController.ForceDeleteGroup)
		}
	}

	return router

}
